// vxeTable.js

import XEUtils from 'xe-utils'
import VXETable from 'vxe-table'

import FilterInput from '../VxeTablePlugins/FilterInput'
import FilterContent from '../VxeTablePlugins/FilterContent'
import FilterComplex from '../VxeTablePlugins/FilterComplex'
// table.js 定义组件
//内容过滤
VXETable.renderer.add('FilterContent', {
    // 不显示底部按钮，使用自定义的按钮
    isFooter: false,
    // 筛选模板
    renderFilter (h, renderOpts, params) {
      return [
        <FilterContent params={ params }></FilterContent>
      ]
    },
    // 重置数据方法
    filterResetMethod ({ options }) {
      options.forEach(option => {
        option.data = { vals: [], sVal: '' }
      })
    },
    // 筛选数据方法
    filterMethod ({ option, row, column }) {
      const { vals } = option.data
      console.log(vals)
      // eslint-disable-next-line no-undef
      let cellValue = XEUtils.get(row, column.property)
      if (column.property === 'customerItemCode') {
        cellValue = cellValue.customerItemCode || cellValue
      }
      return vals.includes(cellValue)
    }
})
// 条件过滤
VXETable.renderer.add('FilterComplex', {
  // 不显示底部按钮，使用自定义的按钮
  isFooter: false,
  // 筛选模板
  renderFilter (h, renderOpts, params) {
      const { events } = renderOpts
      return [
      <FilterComplex params={ params } events={events}></FilterComplex>
      ]
  },
  // 重置数据方法
  filterResetMethod ({ options }) {
      // console.log(options)
      options.forEach(option => {
      option.data = { type: 'lt', name: '' }
      })
  },
  // 筛选数据方法
  filterMethod ({ option, row, column }) {
      // console.log(option, row, column)
      const cellValue = parseFloat(XEUtils.get(row, column.property))
      console.log("cellValue=", cellValue)
      const { name, type } = option.data
      if (!name) {
      return true
      }
      if (type === 'lt') {
      return cellValue < parseInt(name)
      } else if (type === 'eq') {
      return cellValue === parseInt(name)
      } else if (type === 'gt') {
      return cellValue > parseInt(name)
      }
      return false
  }
})
//文本过滤
VXETable.renderer.add('FilterInput', {
// 筛选模板
renderFilter (h, renderOpts, params) {
    // console.log(params)
    return [
    <FilterInput params={ params }></FilterInput>
    ]
},
// 筛选方法
filterMethod ({ option, row, column }) {
    const { data } = option
    const cellValue = row[column.property]
    if (cellValue) {
    return cellValue.indexOf(data) > -1
    }
    return false
}
})
